var groupManagerForm_page={};

groupManagerForm_page.action = {
    initBody:function () {
        groupManagerForm_page.interface.initForm();
        groupManagerForm_page.action.bindEvent();
    },
    bindEvent:function () {
        // 提交按钮的事件
        layui.use(['form'],function () {
            var form = layui.form;
            form.on('submit(saveBtn)', function (data) {
                groupManagerForm_page.action.saveBtnClick();
                return false;
            });
        });
        // 取消按钮的事件
        $("#cancelBtn").click(function () {
            AppComm.utils.iframeCloseSelf();
            return false;
        });
    },

    // 在做action动作时校验数据data
    checkData:function (action, data) {
        var result = AppComm.data.newResult();
        return result;
    },
    initData:function(rowId){
        groupManagerForm_page.data.rowId = rowId;
    },
    saveBtnClick:function () {
        var parentIdValue = $("input[name='parentName_select_nodeId']").val();
        var parentNameValue = $('#parentName_select_input_id').val();
        if (parentIdValue==undefined || parentIdValue==null) {
            parentIdValue = $("#parentId").val();
        }
        if (parentNameValue==undefined || parentNameValue==null) {
            parentNameValue = $("#parentName").val();
        }
        if ($.isEmpty(parentNameValue) && groupManagerForm_page.data.action=='addSubmit') {
            AppComm.layer.warm('上级组织不能为空。');
            return false;
        }
        var params = {
            id:$("#id").val(),
            groupName:$("#groupName").val(),
            comment:$("#comment").val(),
            orderId:$("#orderId").val(),
            parentId:parentIdValue,
            parentName:parentNameValue
        };
        var result = groupManagerForm_page.action.checkData(groupManagerForm_page.data.action, params);
        if (result.code==Enums.ResultEnum().FAILURE.code) {
            AppComm.layer.warm(result.msg);
        } else {
            AppComm.ajax.post('/groupManager/ajax/saveGroup',params, function (resultData) {
                parent.AppComm.ajax.onSuccess(resultData);
                AppComm.utils.iframeCloseSelf();
                parent.groupManager_page.action.initBody();
            });
        }
    },
};

groupManagerForm_page.interface = {
    initForm:function () {
        if (groupManagerForm_page.data.rowId == undefined || '' == groupManagerForm_page.data.rowId.trim()) {
            // 添加
            var html = template("groupManagerForm_template");
            $(".groupManagerForm_box").html(html);

            $("#parentNameInput").css("display","block");
            $("#parentNameShow").css("display","none");

            AppComm.ajax.post("/groupManager/ajax/getGroupList4Dtree", {}, function (resultData) {
                if (resultData.code==Enums.ResultEnum().SUCCESS.code) {
                    layui.config({
                        base: '/static/comm/plugins/dtree/'
                    }).use(['dtree','layer','jquery'],function(){
                        var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
                        dtree.render({
                            elem: "#parentName",
                            width: "100%",
                            dataFormat: "list",
                            data: resultData.data,
                            selectTips: "请选择上级组织",
                            select: true //指定下拉树模式
                        });
                        groupManagerForm_page.data.dtreeIns = dtree;
                    });
                }
            })

            groupManagerForm_page.action.bindEvent();
            groupManagerForm_page.data.action="addSubmit";

        } else {
            // 编辑
            layer.load();
            AppComm.ajax.post("/groupManager/ajax/getGroupById", {
                id:groupManagerForm_page.data.rowId
            },function (resultData) {
                layer.closeAll('loading');
                var html = template("groupManagerForm_template", resultData.data);
                $(".groupManagerForm_box").html(html);

                $("#parentNameInput").css("display","none");
                $("#parentNameShow").css("display","block");

                groupManagerForm_page.action.bindEvent();
                groupManagerForm_page.data.action="editSubmit";
            })
        }
    },
};

groupManagerForm_page.data = {
    'rowId':undefined,
    'dtreeIns':undefined,
};
